/******************************************************************************

    Copyright (C) 2011 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.engine.syspreds;

import java.util.List;

import prettyprolog.datatypes.Substitution;
import prettyprolog.datatypes.Term;
import prettyprolog.engine.Engine;

/**
 * Implements the system predicate namespace/1, that allows to change the current default namespace
 * (where unqualified symbols are interned).
 */
public class Namespace extends SystemPredicate {

	private static final Namespace inst = new Namespace();

	private Namespace() {
		super(1);
	}

	/** Returns the only instance of this system predicate. */
	public static Namespace getInstance() {
		return inst;
	}

	/**
	 * @throws SyspredException
	 *             if the namespace does not exist.
	 */
	public SyspredEffect doCall(Engine en, Substitution subst, List<Term> args)	throws SyspredException {
	    Term arg = args.get(0);
	    String name;
		if(arg instanceof prettyprolog.datatypes.Constant) {
		    name = ((prettyprolog.datatypes.Constant) arg).getSymbol().getName();
		} else if(arg instanceof prettyprolog.datatypes.CharString) {
            name = ((prettyprolog.datatypes.CharString) arg).getString();
        } else if(arg instanceof prettyprolog.datatypes.Variable) {
		    Substitution newSubst = en.getUnificationAlgorithm().unify(arg, prettyprolog.datatypes.Namespace.current());
		    if(newSubst == null) {
		        return new SyspredEffect(true, subst);
		    } else {
		        return new SyspredEffect(false, subst.compose(newSubst));
		    }
		} else {
		    throw new SyspredException("Illegal argument for namespace: " + arg + ". Only constants, strings, and variables are permitted.");
		}
		prettyprolog.datatypes.Namespace namespace = prettyprolog.datatypes.Namespace.get(name);
		if(namespace == null) {
		    throw new SyspredException("Namespace " + name + " does not exist.");
		}
		prettyprolog.datatypes.Namespace.setCurrent(namespace);
		return new SyspredEffect(false, subst);
	}

	public boolean isBacktrackable() {
		return false;
	}

	public String toString() {
		return super.toString() + " assert";
	}
}